package com.cdream.basic.algorithm.leetcode.tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Question107 {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        LinkedList<List<Integer>> res = new LinkedList<>();
        LinkedList<TreeNode> queue = new LinkedList<>();
        if (root==null){
            return res;
        }
        queue.offer(root);
        int count = 1;
        while (!queue.isEmpty()){
            int k = count;
            count = 0;
            List<Integer> ele = new ArrayList<>();
            for (int i = 0; i < k; i++) {
                TreeNode poll = queue.poll();
                ele.add(poll.val);
                if (poll.left!=null){
                    queue.add(poll.left);
                    count++;
                }
                if (poll.right!=null){
                    queue.add(poll.right);
                    count++;
                }
            }
            res.push(ele);
        }
        return res;

    }
}
